/*

 */
package portalsprototype;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.AffineTransform;
import java.util.Date;
import javax.swing.JPanel;

/**
 *
 * @author Oscar
 */
public class Portal extends JPanel {

    private static final int PORTAL_GUIDE_MARGIN = 190;
    private static final int TITLEBAR_HEIGHT = 40;
    private static final Font titleFont = new Font(Font.SANS_SERIF, Font.PLAIN, 26);
    private Screen screen;
    private boolean displayDragForPortalGuides = false;
    private int dragForPortalGuidePosition;
    private int dragForPortalGuidePositionScreen;
    private AffineTransform transform = new AffineTransform();
    private Window currentWindow;
    private boolean isDraggingDivisor = false;
    private boolean displayTime = false;

    public Portal(Screen sc) {
        this.screen = sc;
        this.setBackground(Color.DARK_GRAY);

        final Portal p = this;

        this.addMouseListener(new MouseListener() {

            boolean mousedownOnTitleBar = false;
            long mousedowntime = Long.MAX_VALUE;

            public void mouseClicked(MouseEvent e) {
                // do nothing
            }

            public void mouseEntered(MouseEvent e) {
                // do nothing
            }

            public void mouseExited(MouseEvent e) {
                // do nothing
            }

            public void mousePressed(MouseEvent e) {
                mousedowntime = e.getWhen();

                int button = e.getButton();
                if (e.getY() <= TITLEBAR_HEIGHT) {
                    switch (button) {
                        case MouseEvent.BUTTON1:
                            displayDragForPortalGuides = true;
                            dragForPortalGuidePosition = e.getX();
                            dragForPortalGuidePositionScreen = e.getXOnScreen();

                            // toggle omega bar show/hide
                            break;
                    }
                }

                repaint();
            }

            public void mouseReleased(MouseEvent e) {
                isDraggingDivisor = false;
                displayDragForPortalGuides = false;

                int button = e.getButton();
                switch (button) {
                    case MouseEvent.BUTTON1:
                        // toggle omega bar show/hide
                        break;
                }
                screen.checkPortalWidth();
                repaint();
            }
        });

        this.addMouseMotionListener(new MouseMotionListener() {

            public void mouseDragged(MouseEvent e) {
                if (displayDragForPortalGuides) {
                    if (e.getY() > TITLEBAR_HEIGHT) {
                        displayDragForPortalGuides = false;
                        repaint();
                    }
                    if (e.getXOnScreen() > dragForPortalGuidePositionScreen + PORTAL_GUIDE_MARGIN ||
                            e.getXOnScreen() < dragForPortalGuidePositionScreen - PORTAL_GUIDE_MARGIN) {
                        displayDragForPortalGuides = false;
                        isDraggingDivisor = true;
                        screen.split(e.getX());
                        repaint();
                    }
                }
                if (isDraggingDivisor) {
                    screen.setDivisor(e.getX() + p.getLocation().x);
                }
            }

            public void mouseMoved(MouseEvent e) {
            }
        });
    }

    private void removeDragForPortalGuides() {
        displayDragForPortalGuides = false;
        repaint();
    }
    
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;

        // draw title bar
        int width = this.getWidth();
        int height = this.getHeight();
        g2d.setColor(new Color(200, 50, 20));
        g2d.fillRect(0, 0, width, TITLEBAR_HEIGHT);

        if (displayDragForPortalGuides) {

            g2d.setColor(Color.WHITE);
            int leftGuide = dragForPortalGuidePosition - PORTAL_GUIDE_MARGIN;
            int rightGuide = dragForPortalGuidePosition + PORTAL_GUIDE_MARGIN;
            g2d.drawLine(leftGuide, 0, leftGuide, 20);
            g2d.drawLine(rightGuide, 0, rightGuide, 20);
        }

        if (currentWindow != null) {
            int imageHeight = height - TITLEBAR_HEIGHT;
            transform.scale(width, imageHeight);
            g2d.drawImage(currentWindow.image, 0, TITLEBAR_HEIGHT, width, imageHeight, this);
            
            g2d.setColor(Color.WHITE);
            g2d.setFont(titleFont);
            g2d.drawString(currentWindow.title, 30, 28);
            if (displayTime) {
                g2d.drawString(new Date().toString(), width - 198, 28);
            }
        }
    }

    public void setDisplayTime(boolean displayTime) {
        this.displayTime = displayTime;
    }
    
    public void setCurrentWindow(Window currentWindow) {
        this.currentWindow = currentWindow;
        repaint();
    }

    public void setPosition(int x, int width, int height) {
        this.setBounds(x, 0, width, height);
    }
}
